﻿/**********************************************
系统:Ebada物流企业ERP
模块:示例
作者:Rabbit
创建时间:2011-2-28
最后一次修改:2011-2-28
***********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.Client.Platform;

namespace Ebada.Modules.Document
{

    public partial class UCDocumentsRightRefresh : DevExpress.XtraEditors.XtraUserControl {
        private GridViewOperation<DocumentHistory> gridViewOperation;

        public event SendDataEventHandler<DocumentHistory> FocusedRowChanged;
        private string parentID;
        public UCDocumentsRightRefresh()
        {
            InitializeComponent();
            initImageList();
            initColumns();
            gridViewOperation = new GridViewOperation<DocumentHistory>(gridControl1, gridView1, barManager1, new frmDocumentsEdit());
            gridViewOperation.CreatingObjectEvent +=gridViewOperation_CreatingObjectEvent;
            gridViewOperation.BeforeAdd += new ObjectOperationEventHandler<DocumentHistory>(gridViewOperation_BeforeAdd);
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridView1_FocusedRowChanged);
            HideColums();
            Add_columnName();
            Init_Org();
        }
        /// <summary>
        /// 设置隐藏列
        /// </summary>
        void initColumns() {
            
            //gridView1.Columns["OperateDate"].Caption = "操作时间";
            //gridView1.Columns["DocState"].Visible = false;
            //gridView1.Columns["DocStateName"].Caption = "单证状态";
            //gridView1.Columns["DocCode"].Caption = "单证编号";
            //gridView1.Columns["Act_Date"].Caption = "作业时间";
            //gridView1.Columns["SubmitDateLimit"].Caption = "单证提交时限";
            //gridView1.Columns["SubmitDate"].Caption = "实际提交时间";
            //gridView1.Columns["ValidityDate"].Caption = "有效截至期";
            //gridView1.Columns["RemindDate"].Caption = "有效期预警时间";
            //gridView1.Columns["CheckResult"].Caption = "审查结果";
            //gridView1.Columns["CheckRemark"].Caption = "审查说明";
            //gridView1.Columns["CheckUserID"].Caption = "审查人ID";
            //gridView1.Columns["CheckUserName"].Caption = "审查人";
            //gridView1.Columns["CheckedDate"].Caption = "审查时间";
            //gridView1.Columns["Org_ID"].Caption = "承运商";
            //gridView1.Columns["SpliteFlag"].Visible = false;
            //gridView1.Columns["ScrapDate"].Caption = "报废时间";
            //gridView1.Columns["DocBookID"].Caption = "册子编号";
            //gridView1.Columns["DocBookName"].Caption = "册子名称";
            //gridView1.Columns["DocBookSeq"].Caption = "册子里序列号";
            //gridView1.Columns["CreateUserID"].Caption = "创建人ID";
            //gridView1.Columns["LastEditUserID"].Caption = "修改人ID";
            
        }
        public void HideColums()
        {
            // gridView1.Columns["DocState"].Visible = false;
            gridView1.Columns["DocCode"].Visible = false;
            gridView1.Columns["DocumentTypeID"].Visible = false;
            gridView1.Columns["DocumentPositionID"].Visible = false;
        }
        //将关联单位标识改为关联单位
        //并以中文显示
        public void Init_Org()
        {
            gridView1.Columns["OperateUserName"].Caption = "操作人";
            gridView1.Columns["OperateDate"].Caption = "操作时间";
            gridView1.Columns["OperateType"].Caption = "变更类型";
             gridView1.Columns["DocumentID"].Visible=false;
             gridView1.Columns["Act_Date"].Caption = "作业时间";
             gridView1.Columns["SubmitDateLimit"].Caption = "提交时限";
             gridView1.Columns["SubmitDate"].Caption = "实际提交时间";
             gridView1.Columns["ValidityDate"].Caption = "有效截止日期";
             gridView1.Columns["RemindDate"].Caption = "有效期预警时间";
             gridView1.Columns["CheckResult"].Caption = "审查结果";
             gridView1.Columns["CheckRemark"].Caption = "审查说明";
             gridView1.Columns["CheckUserID"].Visible = false; ;
             gridView1.Columns["CheckUserName"].Caption = "审查人";
             gridView1.Columns["CheckedDate"].Caption = "审查时间";
             gridView1.Columns["ScrapDate"].Caption = "报废时间";
             gridView1.Columns["SpliteFlag"].Caption = "单证表分区标志";

            gridView1.Columns["Org_ID"].Caption = "关联单位";
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit comboBox = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridView1.Columns["Org_ID"].ColumnEdit = comboBox;
            IList<mOrg> org_list = MainHelper.PlatformSqlMap.GetList<mOrg>("");
            comboBox.DataSource = org_list;
            comboBox.DisplayMember = "cnName";
            comboBox.ValueMember = "Org_ID";

        }
        public void Add_columnName()
        {
            gridView1.Columns["DocumentTypeName"].Caption = "单证种类";
            gridView1.Columns["DocState"].Caption = "单证状态";

            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit comboBox = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridView1.Columns["DocState"].ColumnEdit = comboBox;
            IList<mOrg> org_list = MainHelper.PlatformSqlMap.GetList<mOrg>("");
            DataTable temptable = new DataTable();
            temptable.Columns.Add("code", typeof(int));
            temptable.Columns.Add("Name", typeof(string));
            DataRow row1 = temptable.NewRow();
            DataRow row2 = temptable.NewRow();
            DataRow row3 = temptable.NewRow();
            DataRow row4 = temptable.NewRow();
            row1["code"] = 0;
            row1["Name"] = "未提交";

            row2["code"] = 1;
            row2["Name"] = "已提交";

            row3["code"] = 2;
            row3["Name"] = "借出";

            row4["code"] = 3;
            row4["Name"] = "报废";
            temptable.Rows.Add(row1);
            temptable.Rows.Add(row2);
            temptable.Rows.Add(row3);
            temptable.Rows.Add(row4);


            comboBox.DataSource = temptable;
            comboBox.DisplayMember = "Name";
            comboBox.ValueMember = "code";
        }
        void gridViewOperation_BeforeAdd(object render, ObjectOperationEventArgs<DocumentHistory> e)
        {
            if (string.IsNullOrEmpty(parentID)) {
                e.Cancel = true;
                MainHelper.ShowWarningMessageBox("请先选择分类后再增加单证！");
            }
        }
        private void initImageList() {
            ImageList imagelist = new ImageList();
            imagelist.ImageStream = (Ebada.Client.Resource.UCGridToolbar.UCGridToolbarImageList);
            barManager1.Images = imagelist;
        }
        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) {
            if (FocusedRowChanged != null)
                FocusedRowChanged(gridView1, gridView1.GetFocusedRow() as DocumentHistory);
        }
        /// <summary>
        /// 封装了数据操作的对象
        /// </summary>
        [Browsable(false)]
        public GridViewOperation<DocumentHistory> GridViewOperation
        {
            get { return gridViewOperation; }
            set { gridViewOperation = value; }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(DocumentHistory newobj)
        {
            newobj.DocumentHisID = Guid.NewGuid().ToString();
            newobj.DocumentTypeID = parentID;
           
        }
        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >= 0)
            {
                string type = view.GetRowCellDisplayText(e.RowHandle, view.Columns["DocState"]);
                if (type == "0" || type =="未提交")
                {
                    //e.Appearance.BackColor = Color.White;
                }
                if (type == "1"||type =="已提交")
                {
                    e.Appearance.BackColor = Color.LightSkyBlue;
                }
                if (type == "2"||type =="借出")
                {
                    e.Appearance.BackColor = Color.LightSeaGreen;
                }
                if (type == "3" || type == "报废")
                {
                    e.Appearance.BackColor = Color.LightGray;
                }
            }

        }
        /// <summary>
        /// 父表ID
        /// </summary>
        [Browsable(false)]
        public string ParentID {
            get { return parentID; }
            set {
                parentID = value;
                string str=" where 1>1";
                if(!string.IsNullOrEmpty(value)){
                    str = string.Format("where DocumentTypeID='{0}'", value);
                }
                gridViewOperation.RefreshData(str);
            }
        }
    }
}
